package cn.itzd.controller;

import cn.itzd.entity.SysUser;
import cn.itzd.entity.SysUserRole;
import cn.itzd.service.SysUserService;
import cn.itzd.utils.PageUtils;
import cn.itzd.utils.SecurityUtils;
import cn.itzd.utils.result.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (DadaUser)表控制层
 *
 * @author makejava
 */
@RestController
@RequestMapping("system/user")
public class SysUserController extends BaseController{
    /**
     * 服务对象
     */
    @Autowired
    private SysUserService sysUserService;
    @GetMapping
    public R<?> getById() {
        return R.ok(sysUserService.getById(SecurityUtils.getUserId()));
    }
    @GetMapping("/list")
    public R<?> list(SysUser sysUser) {
        PageUtils.start();
        List<SysUser> list = sysUserService.userList(sysUser);
        return R.table(list);
    }

    @GetMapping(value = "/{id}")
    public R<?> query(@PathVariable("id") Long id) {
        return R.ok(sysUserService.getById(id));
    }

    @PostMapping
    public R<?> add(@RequestBody SysUser sysUser) {
        String username = sysUser.getUsername();
        List<SysUser> list = sysUserService.lambdaQuery().eq(SysUser::getUsername, username).list();
        if (list.size()>0) {
            return R.failed("用户名重复");
        }
        return R.ok(sysUserService.add(sysUser));
    }
    @PostMapping("add")
    public R<?> add1(@RequestBody SysUser sysUser) {
        String username = sysUser.getUsername();
        List<SysUser> list = sysUserService.lambdaQuery().eq(SysUser::getUsername, username).list();
        if (list.size()>0) {
            return R.failed("用户名重复");
        }
        return R.ok(sysUserService.add(sysUser));
    }

    @PutMapping
    public R<?> edit(@RequestBody SysUser sysUser) {
        return R.ok(sysUserService.edit(sysUser));
    }
    @PutMapping("pwd")
    public R<?> editPwd(@RequestBody SysUser sysUser) {
        return R.ok(sysUserService.editPwd(sysUser));
    }

    @DeleteMapping("/{id}")
    public R<?> delete(@PathVariable("id") Long id) {
        return R.ok(sysUserService.removeById(id));
    }
    @PutMapping("edit")
    public R<?> edit1(@RequestBody SysUser sysUser) {
        String roleName = sysUser.getRoleName();
        if (!"0".equals(roleName)) {
            Long id = sysUser.getId();
            LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysUserRole::getUserId,id);
            userRoleService.remove(wrapper);
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(id);
            sysUserRole.setRoleId(sysUser.getRoleName());
            userRoleService.save(sysUserRole);
        }
        return R.ok(sysUserService.updateById(sysUser));
    }
    @GetMapping("getById/{id}")
    public R<?> getById(@PathVariable String id) {
        return R.ok(sysUserService.getById(id));
    }
}

